/* C.Strndup: save a string on the heap; return pointer to it */

#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include "utils.h"

char *strndup (const char *str, int n)
{
	char *p = malloc(n+1);

	if (p == NULL)
	{
		fprintf(stderr, "Not enough memory to save string\n");
		exit(1);
	}

	if (n > 0)
		memcpy(p,str,n);

	p[n] = '\0';

	return p;
}
